public with sharing class FinanceAgreementEXT 
{
	List<Finance_Agreement__c> finAgr, finAgrSize;
	public string filters {get;set;}
	public string pageNumbers;
	public string whereClause = '';
	public string queryString = 'SELECT Name, Account__r.Name, Policy__r.Name, Policy__c, Premium__c, Status__c FROM Finance_Agreement__c ';				
	public string queryEnd = ' ';	
	public string searchTerm;
	public string currentUser;
		
	//Pagination Variables
   	private integer counter=0;  //keeps track of the offset
   	private integer list_size=250; //sets the page size or number of rows
   	public integer total_size; //used to show user the total size of the list	
   	
   	public string getpageNumbers()
   	{
   		Long pageCounter;

   		decimal totalPagesDec = (decimal.valueOf(total_size)/250);
   		long tpgs = (total_size/250);
   		integer totalPages = integer.valueOf(totalPagesDec.round(system.roundingMode.Up));
   		
   		if(counter == 0)
   		{
   			pageCounter = 1; 
   		}
   		else
   		{
   			pageCounter = decimal.valueOf((counter/250)).round(system.roundingMode.Up) + 1;
   		}  		   		
   		
   		getfinAgr();
   		pageNumbers = 'Page ' + string.valueOf(pageCounter) + ' of ' + string.valueOf(totalPages);   		
   		return pageNumbers;
   	}   	

	public FinanceAgreementEXT(ApexPages.StandardController controller)
	{
		//total_size = [select count() from Finance_Agreement__c limit 250]; //set the total size in the constructor
		filters = 'My Finance Agreements';	 
		currentUser = system.Userinfo.getUserId();
	} 
	
   	public PageReference newLink() 
   	{
     	return new PageReference('/apex/vfNewFinanceAgreement');
	}	
	
	public List<SelectOption> getFilter()
	{
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('My Finance Agreements', 'My Finance Agreements'));
		options.add(new SelectOption('Active', 'Active'));
		options.add(new SelectOption('Pending', 'Pending'));
		options.add(new SelectOption('Canceled', 'Canceled'));
		options.add(new SelectOption('Paid in full', 'Paid in full'));
		options.add(new SelectOption('On Hold', 'On Hold'));
		options.add(new SelectOption('Deactivated', 'Deactivated'));
		options.add(new SelectOption('Late Notice', 'Late Notice'));
		return options;
	}
	
	public String getFilters()
	{
		return filters;
	}
	
	public void setFilters(String filters)
	{
		this.filters = filters;
	}
	
	public void doSearch()
	{
		filters = 'Custom Search';
		getfinAgr();
	}	
	
	public string getsearchTerm()
	{
		return searchTerm;
	}
	
	public void setsearchTerm(string sTerm)
	{
		searchTerm = sTerm;
	}	

	public List<Finance_Agreement__c> getfinAgr()  
	{
		total_size = 1;	
		string queryEnd = 'Order by Name Limit ' + list_size + ' offset ' + counter;	 
		
		try {	
					
			if(filters == 'Custom Search')
			{
				whereClause = 'Where Name like \'%' +  searchTerm + '%\' Or Account__r.Name like \'%' +  searchTerm + '%\'';
			}
			else if(filters != null)
			{
				whereClause = ' Where Status__c = \'' + filters + '\'';			
			}     
			
			if(filters == 'My Finance Agreements')
			{
				string Owner = ' Where Account__r.OwnerId = \'' + currentUser + '\' ';
				finAgr = database.query(queryString + Owner + ' ' + queryEnd);	
				string qString = queryString + ' Limit 250';
				finAgrSize = database.query(qString );
				total_size  = finAgrSize.size();				 			
			}
			else
			{
				finAgr = database.query(queryString + ' ' + whereClause + ' ' + queryEnd);
				string qString = queryString + ' Limit 250';
				finAgrSize = database.query(qString );
				total_size = finAgrSize.size();				
			}		
		
		return finAgr;
	}
		catch (QueryException e) {
         ApexPages.addMessages(e);   
         return null;
    	}	
	}	
   public PageReference Beginning() { //user clicked beginning
      counter = 0;
      getPageNumbers();
      return null;
   }
 
   public PageReference Previous() { //user clicked previous button
      counter -= list_size;
      getPageNumbers();
      return null;
   }
 
   public PageReference Next() { //user clicked next button
      counter += list_size;
      getPageNumbers();
      return null;
   }
 
   public PageReference End() { //user clicked end
      counter = total_size - math.mod(total_size, list_size);
      getPageNumbers();
      return null;
   }
 
   public Boolean getDisablePrevious() { 
      //this will disable the previous and beginning buttons
      if (counter>0) return false; else return true;
   }
 
   public Boolean getDisableNext() { //this will disable the next and end buttons
      if (counter + list_size < total_size) return false; else return true;
   }
 
   public Integer getTotal_size() {
      return total_size;
   }
 
   public Integer getPageNumber() {
      return counter/list_size + 1;
   }
 
   public Integer getTotalPages() {
      if (math.mod(total_size, list_size) > 0) {
         return total_size/list_size + 1;
      } else {
         return (total_size/list_size);
      }
   }		
}